package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/lexicographically-smallest-negated-permutation-that-sums-to-target/
 * @create 2025/11/23 12:10
 * @since JDK17
 */

public class Solution100 {
    public int[] lexSmallestNegatedPerm(int n, long target) {
        long sum = (1l + n) * n / 2;
        if (sum < Math.abs(target)) {
            return new int[0];
        }
        int[] arr = new int[n];
        for (int i = n - 1; i >= 0; i--) {
            if (sum - target >= 2 * (i + 1)) {
                arr[i] = -(i + 1);
                sum -= 2 * (i + 1);
            } else {
                arr[i] = i + 1;
            }
        }
        if (sum != target) {
            return new int[0];
        }
        return arr;
    }
}
